



var now_type = '';

function uploadDocument(obj) {
	now_type = $(obj).attr('data-type');
	$('#document-file').click();

}

$('#document-file').change(function () {
	uploadFile();
});


function uploadFile() {

	var fileObj = document.getElementById("document-file").files; // js 获取文件对象
	if (fileObj.length == 0) {
		errorAlert('请选择图片');
		return;
	}
	var formFile = new FormData();
	for (var i = 0; i < fileObj.length; i++) {
		formFile.append("myfile[]", fileObj[i]); //加入文件对象
	}
	$('.document-p .progress-bar').css('width', '0%');
	$('.document-p .p-text').html('已上传（0%）');
	$('.document-p').show();
	var data = formFile;
	setTimeout(function () {
		$.ajax({
			url: 'Admin/CommonUpload/upload/' + now_type,
			data: data,
			type: "POST",
			dataType: "json",
			cache: false, //上传文件无需缓存
			processData: false, //用于对data参数进行序列化处理 这里必须false
			contentType: false, //必须
			xhr: xhrOnProgress(function (e) {
				var percent = e.loaded / e.total;
				var now_jindu = (parseFloat(percent) * 100).toFixed(2);
				console.log(now_jindu);
				$('.document-p .progress-bar').css('width', now_jindu + '%');
				$('.document-p .p-text').html('已上传（' + now_jindu + '%）');
				$('.document-p').show();
			}),
			success: function (data) {
				$('.document-p .progress-bar').css('width', '100%');
				$('.document-p .p-text').html('已上传（100%）');
				$('.document-p').hide();
				if (data.status) {
					// alert(now_type);
					var path = JSON.parse(data.v);
					var icon = JSON.parse(data.remark);
					$.each(path, function (k, v) {
						$('.data-box-demo img').attr('src', getRootPath() + '/' + v);
						$('.data-box-demo a').attr('href', getRootPath() + '/' + v);
						$('.data-box-demo .path').val(v).attr('name', 'attend_img[' + now_type + '][]');
						//  $('.data-box-demo img').attr('src', getRootPath() + '/' + v);
						//  $('.data-box-demo a').attr('href', getRootPath() + '/' +v);
						// $('.data-box-demo .path').val(getRootPath() + '/' +v).attr('name', 'attend_img[' + now_type + '][]');
						//  $('.data-box-demo .icon').val(icon[k]).attr('name', 'icon[' + now_type + '][]');
						$('#editForm .td_' + now_type).append($('.data-box-demo').html());
					});
				} else {
					window.parent.topMessagePrompt('error', data.msg);
				}
			}
		})
	}, 1000);
}

var xhrOnProgress = function (fun) {
	xhrOnProgress.onprogress = fun; //绑定监听
	//使用闭包实现监听绑
	return function () {
		//通过$.ajaxSettings.xhr();获得XMLHttpRequest对象
		var xhr = $.ajaxSettings.xhr();
		//判断监听函数是否为函数
		if (typeof xhrOnProgress.onprogress !== 'function')
			return xhr;
		//如果有监听函数并且xhr对象支持绑定时就把监听函数绑定上去
		if (xhrOnProgress.onprogress && xhr.upload) {
			xhr.upload.onprogress = xhrOnProgress.onprogress;
		}
		return xhr;
	}
}

function delDocument(obj) {
	var path = $(obj).parent('.data-box').find('.path').val();
	$(obj).parent('.data-box').remove();
	// $.ajax({
	//     url: "Admin/CommonContract/delDocument",
	//     type: "post",
	//     data: {
	//         path: path
	//     },
	//     dataType: 'json',
	//     success: function(data) {
	//         if (data.status) {

	//         } else {

	//         }
	//     }
	// });
}
// 大图预览
function showImage(obj) {
	var img = {
		data: []
	}
	$(obj).parent().parent().parent().find('.path').each(function () {
		// alert($(this).next().val());
		if ($(this).next().val() == 'img.png') {
			img.data.push({
				'alt': '',
				'pid': (Number($(this).index()) + 1),
				'src': $(this).val(),
				'thumb': ''
			});
		}
	});
	if (img.data.length == 0) {
		window.parent.topMessagePrompt('error', '无图片');
	} else {
		showDialogModalPhoto(img);
	}
}




function delImg(obj) {
	$(obj).prev().prev().attr('src', 'cust/images/upload.png');
	$(obj).prev().prev().prev().val('');
}

function showProduct(obj) {
	window.open($(obj).attr('data-href'));
}



function bannerPrev(obj) {
	var can_prev = $(obj).parents('.data-box').prev().hasClass('data-box-banner');
	if (can_prev) {
		$(obj).parents('.data-box').prev().before($(obj).parents('.data-box'));
	}
}

function bannerNext(obj) {
	var can_next = $(obj).parents('.data-box').next().hasClass('data-box-banner');
	if (can_next) {
		$(obj).parents('.data-box').next().after($(obj).parents('.data-box'));
	}
}